package JDBC;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class B {
    @Test
    public void add(){
        Connection con=null;
        PreparedStatement pre=null;
        //加载到内存中去
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://127.0.0.1/bz?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false","root","root");
            String sql="insert into person(username,password) values(?,?)";
            pre=con.prepareStatement(sql);
            pre.setString(1,"lulu");
            pre.setString(2,"520");
            System.out.println(pre.executeUpdate()>0?"add successful":"add false");

        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(pre!=null){
                try {
                    pre.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    public void delete(){
        Connection con=null;
        PreparedStatement pre=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://127.0.0.1/bz?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false","root","root");
            String sql="delete from person where username=?";
            pre=con.prepareStatement(sql);
            pre.setString(1,"yuanru");
            System.out.println(pre.executeUpdate()>0?"delete successful":"delete false");
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(pre!=null){
                try {
                    pre.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Test
    public void update(){
        Connection con=null;
        PreparedStatement pre=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://127.0.0.1/bz?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false","root","root");
            String sql="update person set username=?,password=?";
            pre=con.prepareStatement(sql);
            pre.setString(1,"feifan");
            pre.setString(2,"777");
//            pre.setString(3,"1");
            System.out.println(pre.executeUpdate()>0?"successful":"false");
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(pre!=null){
                try {
                    pre.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
